package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.PayMentRecordModel;
import com.parking.carsystem.parkingchargesystem.utils.TimeUtil;
import com.parking.carsystem.parkingchargesystem.view.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

public class PayMentRecordActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.not_data)
    ImageView not_data;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.pay_ment_record)
    SwipeRecyclerView payMentRecord;
    //    @BindView(R.id.refresh_layout)
//    SwipeRefreshLayout refreshLayout;
    private EXTRecyclerAdapter<PayMentRecordModel.DataBean> recyclerAdapter;
    private ArrayList<PayMentRecordModel.DataBean> payMentDatas;
    private String plateNumber;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_payment_record;
    }

    @Override
    public void initView() {
//        refreshLayout.setColorSchemeResources(R.color.parking_yellow, R.color.refresh_blue, R.color.refresh_end);
        //设置动画样式下拉的起始点和结束点，scale 是指设置是否需要放大或者缩小动画。
//        refreshLayout.setOnRefreshListener(mRefreshListener); // 刷新监听。
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        payMentRecord.setLayoutManager(new LinearLayoutManager(this));
//        payMentRecord.useDefaultLoadMore(); // 使用默认的加载更多的View。
//        payMentRecord.setLoadMoreListener(mLoadMoreListener); // 加载更多的监听。
        recyclerAdapter = new EXTRecyclerAdapter<PayMentRecordModel.DataBean>(R.layout.item_payment) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, final PayMentRecordModel.DataBean dataBean) {
                TextView invoice_status_msg = extViewHolder.findViewById(R.id.invoice_status_msg);
                TextView order_number = extViewHolder.findViewById(R.id.order_number);
                TextView invoice_number_msg = extViewHolder.findViewById(R.id.invoice_number_msg);
                extViewHolder.setText(R.id.parking_record, dataBean.payTime);
//                extViewHolder.setText(R.id.license_plate, dataBean.plateNumber);
                extViewHolder.setText(R.id.parking_information, dataBean.parkinglotName);
                order_number.setText(dataBean.recordId);
//                extViewHolder.setText(R.id.charging_start_time, dataBean.inTime);
//                extViewHolder.setText(R.id.charging_end_time, dataBean.outTime);
                extViewHolder.setText(R.id.money, "-" + String.valueOf(dataBean.fee));
//                extViewHolder.setText(R.id.charging_money, "-¥" + String.valueOf(dataBean.couponFee));
//                extViewHolder.setText(R.id.charging_duration, TimeUtil.getDistanceTime(dataBean.outTime, dataBean.inTime));
                final RelativeLayout invoice_allmessage = extViewHolder.findViewById(R.id.invoice_allmessage);
                RelativeLayout invoice_number_layout = extViewHolder.findViewById(R.id.invoice_number_layout);
//                RelativeLayout invoice_title_layout = extViewHolder.findViewById(R.id.invoice_title_layout);
                final ImageView show_invoice_message = extViewHolder.findViewById(R.id.show_invoice_message);
                final TextView invoice_open = extViewHolder.findViewById(R.id.invoice_open);
                if (TextUtils.isEmpty(dataBean.invoiceNo)) {
                    invoice_status_msg.setText("未开发票");
                    invoice_number_layout.setVisibility(View.GONE);
//                    invoice_title_layout.setVisibility(View.GONE);
                    invoice_open.setVisibility(View.VISIBLE);
                } else {
                    invoice_status_msg.setText("已开发票");
                    invoice_number_layout.setVisibility(View.VISIBLE);
//                    invoice_title_layout.setVisibility(View.VISIBLE);
                    invoice_open.setVisibility(View.GONE);
                }
                invoice_number_msg.setText(dataBean.invoiceNo);
//                extViewHolder.setText(R.id.invoice_title_msg, dataBean.invoiceTitle);
                show_invoice_message.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击展开
                        if (invoice_allmessage.getVisibility() == View.VISIBLE) {
                            invoice_allmessage.setVisibility(View.GONE);
                            show_invoice_message.setImageResource(R.mipmap.top_zhankai);
                        } else {
                            show_invoice_message.setImageResource(R.mipmap.top_zhankai_press);
                            invoice_allmessage.setVisibility(View.VISIBLE);
                        }
                    }
                });
                invoice_open.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到开票界面
                        Intent intent = new Intent(PayMentRecordActivity.this, InvoiceMessageActivity.class);
                        intent.putExtra("choose", true);
                        intent.putExtra("recordId", dataBean.outTradeNo);
                        startActivityForResult(intent, 0);
                    }
                });
                order_number.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到订单信息详情的界面
                        Intent intent = new Intent(PayMentRecordActivity.this, OrderMessageActivity.class);
                        intent.putExtra("orderRecordId", dataBean.orderRecordId);
                        startActivity(intent);
                    }
                });
                invoice_number_msg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(PayMentRecordActivity.this, InvoiceMessageUrlActivity.class);
                        intent.putExtra("invoiceNo", dataBean.invoiceNo);
                        startActivity(intent);
                    }
                });
            }
        };
        recyclerAdapter.setDatas(payMentDatas);
        payMentRecord.setAdapter(recyclerAdapter);
    }

    @Override
    public void initData() {
        text.setText("缴费记录");
        plateNumber = getIntent().getStringExtra(Constant.PLATENUMBER);
        getOrderList();
    }

    /**
     * 获取历史订单
     */
    private void getOrderList() {
        showLoading();
        OkGo.<String>get(Constant.url + Constant.ORDERLISTPD)
//                .params("plateNumber", plateNumber)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        Gson gson = new Gson();
                        PayMentRecordModel payMentRecordModel = gson.fromJson(response.body(), PayMentRecordModel.class);
                        if (payMentRecordModel.code == 200) {
                            payMentDatas = (ArrayList<PayMentRecordModel.DataBean>) payMentRecordModel.data;
                            if (payMentDatas != null && payMentDatas.size() > 0) {
                                recyclerAdapter.setDatas(payMentDatas);
                                recyclerAdapter.notifyDataSetChanged();
                                not_data.setVisibility(View.GONE);
                            } else {
                                not_data.setVisibility(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    /**
     * 刷新。
     */
    private SwipeRefreshLayout.OnRefreshListener mRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    refreshLayout.setRefreshing(false);

                    // 第一次加载数据：一定要掉用这个方法。
                    // 第一个参数：表示此次数据是否为空，假如你请求到的list为空(== null || list.size == 0)，那么这里就要true。
                    // 第二个参数：表示是否还有更多数据，根据服务器返回给你的page等信息判断是否还有更多，这样可以提供性能，如果不能判断则传true。
                    payMentRecord.loadMoreFinish(false, true);
                }
            }, 3000);
        }
    };
    /**
     * 加载更多。
     */
    private SwipeRecyclerView.LoadMoreListener mLoadMoreListener = new SwipeRecyclerView.LoadMoreListener() {
        @Override
        public void onLoadMore() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // 数据完更多数据，一定要掉用这个方法。
                    // 第一个参数：表示此次数据是否为空。
                    // 第二个参数：表示是否还有更多数据。
                    payMentRecord.loadMoreFinish(false, true);
                }
            }, 3000);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (Constant.MONTHLYSUBSCRIPTIONPARKING == resultCode) {
            getOrderList();
        }
    }
}
